// pages/dictionary/dictionary.js
Page({
  data:{
    index:0,
    search:false,
    z_imgURL:"cloud://test-hf5g2.7465-test-hf5g2-1304459590/image/植物图片/",
    y_imgURL:"cloud://test-hf5g2.7465-test-hf5g2-1304459590/image/药物图片/",
    dictionary:[],
  },
  nextSubmit()
  {
      if (this.data.index < this.data.dictionary.length - 1) {
        // 渲染下一题
        this.setData({
          index: this.data.index + 1
        })
      }
  },
  viewContents()
  {
    wx.navigateBack({
      url: '../dictRoot2/dictRoot2',
    })
  },
    previous()
  {
    if (this.data.index > 0) {
      // 渲染上一题
      this.setData({
        index: this.data.index - 1
      })
    }
  },
  onLoad:function(options)
  {
  let that=this;
  //  console.log(options.index)
  //  console.log(options.dictionary)
   let dictionary=JSON.parse(options.dictionary);
   that.setData({
     dictionary:dictionary,
     index:options.index,
     search:options.search,
     z_imgURL:that.data.z_imgURL+dictionary[options.index].chapter+"/"+dictionary[options.index].section+"/"+dictionary[options.index].name+".jpg",
     y_imgURL:that.data.y_imgURL+dictionary[options.index].chapter+"/"+dictionary[options.index].section+"/"+dictionary[options.index].name+".jpg"
   })
   console.log(dictionary)
  },
  z_exchangeImage(e){
    let that=this
    let URL=that.data.z_imgURL.split('.')
    URL[2]="webp"
    URL=URL.join('.')
    console.log(e)
    that.setData({
      z_imgURL:URL
    })
  },
  y_exchangeImage(e){
    let that=this
    let URL=that.data.y_imgURL.split('.')
    URL[2]="webp"
    URL=URL.join('.')
    console.log(e)
    that.setData({
      y_imgURL:URL
    })
  },
  addList(){
    wx.showModal({
      title: '提示',
      content: '添加该中药到今日学习任务吗？',
      success: (res)=> {
        if (res.confirm) {//这里是点击了确定以后
          console.log('用户点击确定')
          let object=wx.getStorageSync('addList')
          let i=this.data.index
          let dictionary=this.data.dictionary
          console.log()
          if(object==''){
            let addItem=JSON.stringify(dictionary[i])
            object=addItem
          }else{
            let addItem=JSON.stringify(dictionary[i])
            object=`${object},${addItem}`
            console.log(object)
          }
          wx.setStorageSync('addList', object)
          wx.navigateBack({
            delta: 1,
          })
        } else {
          console.log('用户点击取消')
        }
      }
    })
  }
})